<?php class version extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function truncate(){
    	return $this->db->truncate('version');
    }
    
    
    function insert_for_excel($array){
//        $this->name = $array['name'];
//        $this->category_id = $array['category_id'];
//        $this->mod_id = $array['mod_id'];       
//        $this->createtime = time();
       $array['createtime'] = time();
       $re = $this->getversion_array(array('mod_id'=>$array['mod_id'],'category_id'=>$array['category_id'],'name'=>$array['name']));
     //  echo '<pre>';print_r($this);exit;
       if(count($re)==0){
         $this->db->insert('version',$array);
         $result = $this->db->insert_id();
       }else{
         $result = $re[0]->version_id;
       }
       return $result;
    }
    
    function get_last_verison(){
       $this->db->select('category_id');
       $this->db->select_max('name');
       $this->db->group_by('category_id');
       $this->db->order_by("version_id", "asc");
       $query = $this->db->get('version');
       $result = $query->result(); 
       $cout = count($result);
        $str=array();
       for($i=0;$i<$cout;$i++){
       	  $f = $this->getversion_array(array('category_id'=>$result[$i]->category_id,'name'=>$result[$i]->name));
          $str[]=array('version_id'=>$f[0]->version_id,'category_id'=>$result[$i]->category_id);          
       }   
       return $str;
    }
    
   function get_new_version($mod){
   	    $this->db->select('name,version_id');
	   	$this->db->select_min('name');
	   	$this->db->where($mod);
	   	$query = $this->db->get('version');
	   	$result = $query->result();
	   	return $result;
   }
   
   function get_last_version($mod){
   	$this->db->select('name');
   	$this->db->select_max('name');
   	$this->db->where($mod);
   	$query = $this->db->get('version');
   	$result = $query->result();
   	return $result;
   }
    
   function get_lastverison($mod){
       $this->db->select('category_id,mod_id');
       $this->db->select_max('name');
       $this->db->where($mod);
       $this->db->group_by('category_id');
       $this->db->order_by("version_id", "asc");
       $query = $this->db->get('version');
       $result = $query->result(); 
       $cout = count($result);
        $str=array();
       for($i=0;$i<$cout;$i++){
       	  $f = $this->getversion_array(array('category_id'=>$result[$i]->category_id,'name'=>$result[$i]->name));
          $str[]=array('version_id'=>$f[0]->version_id,'category_id'=>$result[$i]->category_id,'mod_id'=>$f[0]->mod_id);          
       }   
       return $str;
    }
    
    
    function insert(){
       $this->mod_id = $_POST['mod_id'];
       $this->des = $_POST['des'];
       $this->category_id = $_POST['category_id'];
       $this->name = $_POST['name'];
       $this->createtime = time();
       $re = $this->getlast($_POST['name'],$_POST['category_id']);
       if(count($re)==0){
         $result = $this->db->insert('version',$this);
       }else{
         $result = true;
       }
       return $result;
    }
    
    
    function insert_by_array($array){ 
       unset($array['createtime']);  
       $this->db->where($array);
       $query = $this->db->get('version');
       $result = $query->result();
       $cout = count($result);
       if($cout>=1){
       	   return $result[0]->version_id;
       }else{
       	   $array['createtime'] = time();
	       $result = $this->db->insert('version',$array);
	       return $this->db->insert_id();
       }
    }
    
    
    function get_id($version){
        $we = array(
                     'mod_id'=>$version['mod_id'],
                     'category_id'=>$version['category_id'],
                     'name'=>$version['name']
        );
        $this->db->where($we);
        $query = $this->db->get('version');
        $result = $query->result();
        $count = count($result);
        if($count==0){
          $this->db->insert('version',$version);
          $version_id = $this->db->insert_id();
        }else{
          $version_id = $result[0]->version_id;
        }
        return $version_id;
    }
    
    
    function getcount($id,$version_id=''){
    	$where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('version');
        $result = $query->result(); 
        return $result;
    }
    
    function getversion_array($arr){
       $this->db->where($arr);
       $this->db->order_by("name", "asc");
       $query = $this->db->get('version');
       $result = $query->result(); 
       return $result;
    }
    
    
    function getlast($name,$id){
       $this->db->where(array('category_id'=>$id,'name'=>$name));
       $query = $this->db->get('version');
       $result = $query->result(); 
       return $result;
    }
    
    function  getvalidversion($id){
       $this->db->limit(1);
       $this->db->where(array('category_id'=>$id));
       $this->db->order_by("name", "desc");
       $query = $this->db->get('version');
       $result = $query->result(); 
       return $result;
    }
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where($id);
       }
       $query = $this->db->get('version');
       
       $result = $query->result(); 
       return $result;
    }
    
    function select2($id,$num="",$offset=""){
       $this->db->select("*");
       $this->db->from("category");
       $this->db->join('version', 'version.category_id = category.category_id');
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where($id);
       }
       if(!isset($id['version.category_id'])){
          $this->db->order_by('category.codes asc,version.name desc');
       }
       $query = $this->db->get();
       
       $result = $query->result(); 
       return $result;
    }
    
    function getversion_bycategory_id($id,$order='asc'){
    	$this->db->where(array('category_id'=>$id));
    	$this->db->order_by("name", $order);
    	$query = $this->db->get('version');    	 
        $result = $query->result(); 
        return $result;
    }
    
    function auth($name,$category_id){
    	$this->db->where(array('name'=>trim($name),'category_id'=>$category_id));
    	$this->db->from('version');
        $result = $this->db->count_all_results(); 
        return $result;
    }
    
    
    function update($data,$id){
       $result = $this->db->update('version',$data,array('version_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('version',array('version_id'=>$id));
       return $result;
    }
}